﻿using Aliyun.OSS;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Common.Utility
{
    public class UploadUtils
    {
        public static UploadResult FileUpload(IFormFile fileUpload, string physicalPath, string folderPath)
        {
            var fileName = "";

            try
            {
                string fullfilename = fileUpload.FileName;
                string strExtension = fullfilename.Substring(fullfilename.LastIndexOf("."));
                string name = CheckFolder(physicalPath, folderPath) + GetFileName();
                fileName = name + strExtension;
                using (var stream = new FileStream(physicalPath + fileName, FileMode.Create))
                {
                    fileUpload.CopyTo(stream);
                }

            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.Message, ex);
                return new UploadResult { Code = -1, Msg = "上传文件异常" };
            }
            return new UploadResult { Data = fileName };
        }
        public static UploadResult OSSFileUpload(IFormFile fileUpload, string folderPath)
        {

            try
            {
                string fullfilename = fileUpload.FileName;
                string strExtension = fullfilename.Substring(fullfilename.LastIndexOf("."));
                string fileName = $"{folderPath}/{DateTime.Now.ToString("yyyyMMdd")}/{GetFileName()}{strExtension}";

                // 构造OssClient实例。
                var ossClient = new OssClient(AppSetting.GetAppSetting("Aliyun:OSS:Endpoint"),
                AppSetting.GetAppSetting("Aliyun:OSS:AccessKeyId"), AppSetting.GetAppSetting("Aliyun:OSS:AccessKeySecret"));
                using var stream = fileUpload.OpenReadStream();
                // 上传文件。
                var result = ossClient.PutObject(AppSetting.GetAppSetting("Aliyun:OSS:BucketName"), fileName, stream);
                return new UploadResult { Data = fileName };

            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.Message, ex);
                return new UploadResult { Code = -1, Msg = "上传文件异常" };
            }
        }
        public static UploadResult OSSFileUpload(string localFilename, string folderPath)
        {
            try
            {
                string strExtension = localFilename.Substring(localFilename.LastIndexOf("."));
                string fileName = $"{folderPath}/{DateTime.Now.ToString("yyyyMMdd")}/{GetFileName()}{strExtension}";

                // 构造OssClient实例。
                var ossClient = new OssClient(AppSetting.GetAppSetting("Aliyun:OSS:Endpoint"),
                AppSetting.GetAppSetting("Aliyun:OSS:AccessKeyId"), AppSetting.GetAppSetting("Aliyun:OSS:AccessKeySecret"));
                // 上传文件。
                var result = ossClient.PutObject(AppSetting.GetAppSetting("Aliyun:OSS:BucketName"), fileName, localFilename);
                return new UploadResult { Data = fileName };

            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.Message, ex);
                return new UploadResult { Code = -1, Msg = "上传文件异常" };
            }
        }

        /// <summary>
        /// 生成日期文件夹
        /// </summary>
        /// <param name="folderPath">原始路径</param>
        /// <returns>返回增加日期路径</returns>
        public static string CheckFolder(string physicalPath, string folderPath)
        {
            //命名一个今天的文件夹     
            string p = DateTime.Now.ToString("yyyyMMdd");
            string folder = "/Upload/" + folderPath + "/" + p + "/";
            //判断文件是否存在     
            if (!System.IO.Directory.Exists(physicalPath + folder))
            {
                //自动生成文件夹       
                System.IO.Directory.CreateDirectory(physicalPath + folder);
            }
            //生成后返回文件夹名     
            return folder;
        }

        public static string GetFileName()
        {
            Random rd = new Random();
            StringBuilder serial = new StringBuilder();
            serial.Append(DateTime.Now.ToString("yyyyMMddHHmmss"));
            serial.Append(rd.Next(1000, 9999).ToString());
            return serial.ToString();

        }
    }
    public class UploadResult
    {
        public int Code { get; set; } = 0;
        public string Msg { get; set; }
        public object Data { get; set; }
    }
}
